//
//  ByCellGoodsDefine.m
//  mijifen_ios
//
//  Created by mc on 2017/11/6.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByCellGoodsDefine.h"

@interface ByCellGoodsDefine()<UITextFieldDelegate>
@end

@implementation ByCellGoodsDefine
{
    UIView      *_headerView;
//    UILabel     *_titleLabel;
    UITextField *_textField;
    UILabel     *_addLabel;
    
    UIView      *_buttonBgView;
    
    NSMutableArray  *_buttonArray;
}

+ (instancetype)cellWithTableView:(UITableView*)tableView{
    
    static NSString *cellIdentifier = @"ByCellGoodsDefine";
    ByCellGoodsDefine *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [self createViews];
    }
    return self;
}

- (void)createViews{
    _headerView = [[UIView alloc] init];
    [self addSubview:_headerView];
    [_headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
        make.top.equalTo(self.mas_top);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH - 30, 30));
    }];
    _headerView.backgroundColor = [UIColor whiteColor];
    
    _addLabel = [[UILabel alloc] init];
    [_headerView addSubview:_addLabel];
    [_addLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(0));
        make.top.equalTo(@(0));
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    _addLabel.text = @"+";
    _addLabel.font = FONT(30);
    _addLabel.textAlignment = NSTextAlignmentCenter;
    _addLabel.textColor = [UIColor lightGrayColor];
    _addLabel.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(addClasssify)];
    [_addLabel addGestureRecognizer:tap];
    
//    _titleLabel = [[UILabel alloc] init];
//    [_headerView addSubview:_titleLabel];
//    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.equalTo(_addLabel.mas_left).offset(-10);
//        make.left.equalTo(@(0));
//        make.top.equalTo(@(0));
//        make.height.mas_equalTo(@(30));
//    }];
//    _titleLabel.font = FONT(16);
//    _titleLabel.textAlignment = NSTextAlignmentCenter;
//    _titleLabel.textColor = [UIColor lightGrayColor];
    _textField = [[UITextField alloc] init];
    [_headerView addSubview:_textField];
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(_addLabel.mas_left).offset(-10);
        make.left.equalTo(@(0));
        make.top.equalTo(@(0));
        make.height.mas_equalTo(@(30));
    }];
    _textField.font = FONT(16);
    _textField.textAlignment = NSTextAlignmentCenter;
    _textField.textColor = [UIColor lightGrayColor];
    _textField.delegate = self;
    _textField.userInteractionEnabled = NO;
    
    _buttonBgView = [[UIView alloc] init];
    [self addSubview:_buttonBgView];
    [_buttonBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_headerView.mas_left);
        make.top.equalTo(_headerView.mas_bottom).offset(10);
        make.right.equalTo(_headerView.mas_right);
        make.bottom.equalTo(self.mas_bottom);
    }];
    
}

- (void)setTitle:(NSString *)title{
//    _titleLabel.text = title;
    _textField.placeholder = title;
}

- (void)setItems:(NSMutableArray *)items{

    float xOffset = 10.0;
    float yOffset = 5.0;
    
    for (UIView *view in _buttonBgView.subviews) {
        if ([view isKindOfClass:[UIButton class]]) {
            [view removeFromSuperview];
        }
    }
    for (int i = 0; i < items.count; i++) {
        
        NSString *strTitle = items[i];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:strTitle forState:UIControlStateNormal];
        [btn.titleLabel setFont:FONT(16)];
        
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        
        CGFloat title_width = [strTitle getSizeOfString:14].width + 30;
        NSLog(@"%@----%lf",items[i],yOffset);
        if (xOffset + title_width > SCREEN_WIDTH - 30 - 20) {
            xOffset = 10;
            yOffset += 30 + 5;
        }
        btn.tag = 100 + i;
        [_buttonBgView addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@(xOffset));
            make.top.equalTo(@(yOffset));
            make.size.mas_equalTo(CGSizeMake(title_width, 30));
        }];
        xOffset += title_width + 8;
        btn.layer.cornerRadius = 30 / 5;
        btn.layer.borderWidth = 0.6;
        
        [_buttonArray addObject:btn];
        if ([self isThisButtonExist:btn :items]) {
            [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
            [btn setBackgroundColor:[UIColor whiteColor]];
        }else{
            [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [btn setBackgroundColor:BY_TEXT_COLOR];
            
        }
        
    }

    
}
#pragma -mark- 是否已经存在
- (BOOL)isThisButtonExist:(UIButton *)button :(NSMutableArray *)array{
    for (NSString *title in array)
    {
        if ([title isEqualToString:button.titleLabel.text]){
            return YES;
        }
    }
    return NO;
}

+ (CGFloat)getHeightFromData:(NSArray *)aryData{
    
    float xOffset = 10.0;
    float yOffset = 5.0;
    
    for (int i = 0; i < aryData.count; i++) {
        
        NSString *strTitle = aryData[i];
        
        CGSize titleSize = [strTitle getSizeOfString:14];
        
        if (xOffset + titleSize.width + 30 > SCREEN_WIDTH - 30 - 20) {
            xOffset = 10;
            yOffset += 30 + 5;
        }
        
        xOffset += titleSize.width + 30 + 8;
    }
    return yOffset + 35;
}

- (void)btnAction:(UIButton *)btn{
    btn.selected = !btn.selected;
    if (btn.selected) {
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn setBackgroundColor:BY_TEXT_COLOR];
    }else{
        [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [btn setBackgroundColor:[UIColor whiteColor]];
    }
}
#pragma mark - 添加分类
- (void)addClasssify{
    _textField.userInteractionEnabled = YES;
    [_textField becomeFirstResponder];
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField.text.length != 0) {
        if (self.editTap) {
            self.editTap(textField.text);
        }
        textField.text = @"";
    }

}

@end
